package StudyCode.summary.mycollection;

import java.lang.reflect.Array;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ReturnsAnArrayOfTheSameSizeAsTheIndexArrayAndTheClassOfTheOriginalArrayWhichContainsTheElementsOfTheOriginalArraySelectedByTheCorrespondingIndices {

	public static int[] selectByIndexArray(int[] originalArray,
	                                       int[] indexArray) {
		int[] selectedElements = (int[]) Array.newInstance(Integer.TYPE,
				indexArray.length);
		for (int i = 0; i < indexArray.length; i++) {
			selectedElements[i] = originalArray[indexArray[i]];
		}
		return selectedElements;
	}

	@SuppressWarnings("unchecked")
	private static <T> T[] unsafeCastArray(Object[] a) {
		return (T[]) a;
	}
}
